package 数组题目.数组变换;

import java.util.Arrays;

//删除有序数组的重复项
// https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
class 删除有序数组的重复项 {
    //我的双指针
    public int removeDuplicates1(int[] nums) {
        int slow = 0;
        int fast = 1;
        // 游标，用于标记更新数组所在的位置
        int cur = 0;
        int delCount = 0;
        while (fast < nums.length) {
            if (nums[fast] != nums[slow]) {
                slow++;
                fast++;
                // 更新数组值
                nums[cur++] = nums[slow];
            } else {
                while (fast < nums.length && nums[fast] == nums[slow]) {
                    fast++;
                }
                nums[cur++] = nums[slow];
                delCount = delCount + fast - slow - 1;
                slow = fast;
                fast++;
            }
        }
        //循环结束 判断最后一段是不是连续重复项，如果不是就添加最后一项
        if (slow == nums.length - 1 && nums[cur] != nums[slow]) {
            nums[cur] = nums[slow];
        }
        return nums.length - delCount;
    }

    //别人的
    public int removeDuplicates2(int[] nums) {
        int index = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != nums[i - 1]) {
                nums[index++] = nums[i];
            }
        }
        return index;
    }

    //别人的
    public int removeDuplicates(int[] nums) {
        int[] uniqueNums = Arrays.stream(nums).distinct().toArray();
        System.arraycopy(uniqueNums, 0, nums, 0, uniqueNums.length);
        return uniqueNums.length;
    }

//    // 我是一个小傻子
//    public static void main(String[] args) {
//        Solution removeDuplicates = new Solution();
//        System.out.println(removeDuplicates.removeDuplicates(new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}));
//    }
}
